package com.sunwayhorizo.health.platform.security.dao.impl;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.platform.base.BaseManager;
import com.sunwayhorizo.health.platform.security.dao.SecurityPermissionDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityPermission;
import com.sunwayhorizo.health.platform.security.persistent.SecurityPermissionRole;

@Stateless
@Name("SecurityPermissionDao")
public class SecurityPermissionImpl extends BaseManager implements SecurityPermissionDao{

	@Override
	public void deleteSecurityPermissionByResourceId(String resourceId) {
		super.entityManager2.createQuery("delete from SecurityPermission sp where sp.resource.resourceId = :resourceId").setParameter("resourceId", resourceId).executeUpdate();
		super.flushClear(entityManager2);
	}
	
	
	@Override
    public void deleteSecurityPermissionByResourceIds(List<String> resourceIds) {
        super.entityManager2.createQuery("delete from SecurityPermission sp where sp.resource.resourceId in :resourceId").setParameter("resourceId", resourceIds).executeUpdate();
        super.flushClear(entityManager2);
    }
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermissionRole> findSecurityPermissionListByRoleId(String roleId) {
		String jpql = "select distinct spr from SecurityPermissionRole spr " +
					  "where spr.role.roleId=:roleId ";
		return super.entityManager2.createQuery(jpql).setParameter("roleId", roleId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermission> findSecurityPermissionListByAccountIdJoinRole(String accountId) {
		String jpql = "select distinct spr.permission from SecurityPermissionRole spr,SecurityAccountRole sar  " +
		  			  "where spr.role = sar.role and sar.account.accountId =:accountId";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermission> findSecurityPermissionListByAccountIdJoinRoleWithScreen(String accountId) {
		String jpql = "select distinct spr.permission from SecurityPermissionRole spr,SecurityAccountRole sar  " +
		  "where spr.role = sar.role and sar.account.accountId =:accountId " +
		  "and not exists( " +
		  " select sarp.arpId from SecurityAccountRolePermission sarp " +
		  " where spr.role = sarp.role " +
		  " and spr.permission = sarp.permission " +
		  " and sar.account = sarp.account " +
		  ")";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermission> findSecurityPermissionListByAccountId(String accountId) {
		String jpql = "select distinct spa.permission from SecurityPermissionAccount spa " +
		  			  "where spa.account.accountId=:accountId ";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermission> findSecurityPermissionListByAccountIdWithScreen(String accountId) {
		String jpql = "select distinct spa.permission from SecurityPermissionAccount spa " +
		  			  "where spa.account.accountId=:accountId " +
		  			  "and not exists( " +
		  			  " select sarp.arpId from SecurityAccountRolePermission sarp " +
		  			  " where spa.account = sarp.account " +
		  			  " and spa.permission = sarp.permission " +
		  			  ")";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermission> findSecurityPermissionListByResourceId(String resourceId) {
		return super.entityManager2.createQuery("select sp from SecurityPermission sp where sp.resource.resourceId = :resourceId").setParameter("resourceId", resourceId).getResultList();
	}
	
	@Override
	public Long findSecurityPermissionListSizeByOperationId(String operationId){
		String jpql = "select count(sp.permissionId) from SecurityPermission sp where sp.operation.operationId =:operationId ";
		return (Long)super.entityManager2.createQuery(jpql).setParameter("operationId", operationId).getSingleResult();
	}
}
